package edu.zzuli.assistant.action;

import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
import edu.zzuli.assistant.bean.Notice;
import edu.zzuli.assistant.service.NoticeService;
import edu.zzuli.assistant.service.impl.NoticeServiceImpl;
public class NoticeAction extends ActionSupport implements SessionAware{


	private NoticeService noticeService =new NoticeServiceImpl();
	private List<Notice> noticeList;
	private Notice notice ;
	private int page =1;//评论当前页数 默认第一页
	private String picList[];
	private Map<String,Object> session;
	
	public String[] getPicList() {
		return picList;
	}

	public void setPicList(String[] picList) {
		this.picList = picList;
	}
	public List<Notice> getNoticeList() {
		return noticeList;
	}
	public void setNoticeList(List<Notice> noticeList) {
		this.noticeList = noticeList;
	}
	
	@Override
	public void setSession(Map<String, Object> session) {
		// TODO Auto-generated method stub
		this.session=session;
	}

	
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public Notice getNotice() {
		return notice;
	}
	public void setNotice(Notice notice) {
		this.notice = notice;
	}
	
	public String detail(){
		notice = noticeService.selectById(notice.getId());
		if(notice.getNoticePics()!=null){
			picList = notice.getNoticePics().split(";");
		}
		if(notice!=null){
			return "detail_success";
		}
		return null;
	}
	
	// 列表
	public String list(){
		noticeList=noticeService.getNoticList(page, 8);
		return "list_success";
	}
	
	public String adList(){
		if(session.get("id")!=null){
			noticeList=noticeService.getNoticList(page, 8);
			return "adlist_success";
		}
		return "fail";
	}
	//管理员删除管理员发布的通告(notice_delete.action)
	public String delete(){
		if(session.get("id")!=null){
			boolean result=noticeService.deleteById(notice.getId());
			if(result){
				return "delete_success";
			}
		}
		return  "fail";
	}
	
	// 发布
	public String send() {
		if (session.get("id") != null) {
			// 管理员
			if (notice.getTitle()==null || notice.getTitle().length() == 0 || notice.getTitle().length() > 35) {
				return "comment_replyValidate";
			} else {
				String imagePath = notice.getNoticePics();
				if (imagePath != null  && imagePath.length()>2) {
					imagePath = imagePath.substring(1, imagePath.length());
					notice.setNoticePics(imagePath);
				}else{
					notice.setNoticePics(null);
				}
			}
			String name = session.get("name").toString();
			String id = session.get("id").toString();
			notice.setAdminId(id);
			notice.setAuthorName(name);
			notice.setPostTime(new Date());
			boolean flag = noticeService.publishNotice(notice);
			if(flag){
				return "post_success";
			}else{
				return "post_fail";
			}
		} else {
			// 没有登录 跳转到首页
			return "fail";
		}
	}
}
